﻿using FineUI;
using System;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Donation;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.Donation
{
    public partial class DonationEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "DonationAdd";
            }
        }
        #endregion

        #region 页面属性
        public string EditorContent
        {
            get
            {
                string id = GetQueryIntValue("id");
                if (!string.IsNullOrEmpty(id))
                {

                    PartyDonation model = DB.PartyDonations.Find(id);
                    return model.Content;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(ddlType, SysDataDictionary.DonationType);
            ControlBusiness.ControlBindDataDictionary(ddlState, SysDataDictionary.DonationState);
            ControlBusiness.ControlBindDataDictionary(ddlDName, SysDataDictionary.DonationDName);
            ControlBusiness.ControlBindDataDictionary(ddlDFrom, SysDataDictionary.DonationDFrom);
            ControlBusiness.ControlBindDataDictionary(ddlPurpose, SysDataDictionary.DonationPurpose);
            ControlBusiness.ControlBindDataDictionary(ddlName, SysDataDictionary.DonationName);
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyDonation model = DB.PartyDonations.Find(id);
                if (model == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtName.Text = model.Name;
                ddlName.SelectedValue = model.Name;
                txtNum.Text = model.Num.ToString();
                txtEffectiveDate.SelectedDate = model.EffectiveDate;
                ddlDFrom.SelectedValue = model.DFrom;
                ddlPurpose.SelectedValue = model.Purpose;
                ddlDName.SelectedValue = model.DName;
                txtDPerson.Text = model.DPerson;
                txtLinkMan.Text = model.LinkMan;
                txtContactInformation.Text = model.ContactInformation;
                txtAddress.Text = model.Address;
                txtMapCoordinates.Text = model.MapCoordinates;
                txtDExplain.Text = model.DExplain;
                ddlState.SelectedValue = model.State.ToString();
                ddlType.SelectedValue = model.Type;

                BindControl(model.Type);
            }
            else
                BindControl("");

        }
        #endregion

        #region Method

        private void BindControl(string _type)
        {
            //若类型为服务时，加载名称下拉控件
            if (_type == "3")
            {
                txtName.Hidden = true;
                txtName.Text = "";
                ddlName.Hidden = false;
                txtName.Readonly = false;
            }
            else if (_type == "1" || _type == "")
            {
                txtName.Hidden = false;
                txtName.Readonly = true;
                txtName.Text = "资助金";
                ddlName.Hidden = true;
            }
            else
            {
                txtName.Hidden = false;
                txtName.Text = "";
                ddlName.Hidden = true;
                txtName.Readonly = false;
            }
        }

        private string GetNameValue(string _type)
        {
            string result = "";

            //若类型为服务时，加载名称下拉控件
            if (_type == "3")
                result = ddlName.SelectedValue;
            else if (_type == "1" || _type == "")
                result = txtName.Text;
            else
                result = txtName.Text;

            return result;
        }
        #endregion

        #region Events

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindControl(ddlType.SelectedValue);
        }

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            PartyDonation model;

            if (id != "")
            {
                //更新
                model = DB.PartyDonations.Find(id);
                if (model == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                //DB.SaveChanges();
            }
            else
            {
                model = new PartyDonation();
                model.ID = StringHelper.NewID;
                model.CreateTime = DateTime.Now;
                model.CreateBy = CurrentUser.LoginID;
                model.State = Convert.ToInt32(ddlState.SelectedValue);

                DB.PartyDonations.Add(model);
            }

            model.Type = ddlType.SelectedValue;
            model.Name = GetNameValue(ddlType.SelectedValue);
            model.Num = Convert.ToInt32(txtNum.Text);
            model.EffectiveDate = Convert.ToDateTime(txtEffectiveDate.Text);
            model.DFrom = ddlDFrom.SelectedValue;
            model.Purpose = ddlPurpose.SelectedValue;
            model.Content = this.Request.Form["editorValue"];
            model.DName = ddlDName.SelectedValue;
            model.DPerson = txtDPerson.Text;
            //model.DOrganization = ddlDOrganization.SelectedValue;
            model.LinkMan = txtLinkMan.Text;
            model.ContactInformation = txtContactInformation.Text;
            model.Address = txtAddress.Text;
            model.MapCoordinates = txtMapCoordinates.Text;
            model.DExplain = txtDExplain.Text;

            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}